<?php

namespace App\Http\Controllers;

use DB;
use Hash;
use HiCommon\MongodbModel\MongoTest;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Schema;

class IndexController extends Controller
{

    /**
     * 测试 mongodb
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function test_mongodb(){
        /* mongodb 操作*/
        $res = MongoTest::insert(['name'=>'云逸','sex'=>'男']);
        $res = MongoTest::take(5)->get()->toarray();
        dd($res);
    }

    /**
     * 测试 redis
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function test_redis(){
        /* redis 操作*/
        Redis::set('name', 'guwenjie');
        Redis::set('name1', 'yunyi');
        $values = Redis::get('name1');
        dd($values);
    }

    /**
     * 创建用户表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function create_user_table()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('username', 30)
                  ->unique()
                  ->comment('用户名')
                  ->default('');
            $table->string('password', 150)
                  ->comment('用户密码')
                  ->default('');
            $table->tinyInteger('status')
                  ->comment('用户状态：1正常 2禁用')
                  ->default(1);
            $table->string('nickname', 30)
                  ->comment('用户昵称')
                  ->default('');
            $table->string('email', 50)
                  ->comment('用户邮箱')
                  ->default('');
            $table->string('phone', 15)
                  ->comment('手机号')
                  ->default('');
            $table->string('remember_token', 100)
                  ->comment('token')
                  ->default('');
            $table->timestamp('email_verified_at', 1)
                  ->nullable()
                  ->comment('邮箱认证时间');
            //            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * 添加用户
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_user(){
        DB::table('users')
          ->insert(['username' => 'wu', 'password' => Hash::make('wu555662')]);
    }


    /**
     * 测试 接口登录
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_login(){

    }

    /**
     * 测试 接口登录
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function login(){
//        return ['state'=>0,'msg'=>'please go to login','data'=>[],'error'=>'0'];

    }

    /**
     * 测试 接口获取用户信息
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_me(){
        return response()->json($this->guard()->user());

    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\Guard
     */
    public function guard()
    {
        return Auth::guard('api');
    }
}
